<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计数事件</title>
</head>
<body>

<script>
//    increment();
//    var counter =0;
//    function increment()
//    {
//        counter = counter + 1;
//        console.log('Number of events:' + counter);
//    }

    /**
     * 返回函数不仅记住了局部变量 counter，而且记住了传递进来的参数。
     * @param counterName
     * @returns {increment}
     */
    function createCounter(counterName)
    {
        var counter = 0;

        function display()
        {
            console.log('Number of events:'+counterName +":" +counter);
        }

        function increment()
        {
            counter = counter+1;
            display();
        }
        function decrement()
        {
            counter = counter -1;
            display();
        }
        return {
            increment:increment,
            decrement:decrement
        };
    }
    var counter1 = createCounter('cats');
    var counter2 = createCounter('dogs');
    counter1.increment();
    counter1.increment();
    counter2.increment();
    counter1.increment();

    function createStack()
    {
        var elements = [];
        return {
            push:function(el){elements.unshift((el));},
            pop:function(){return elements.shift();}
        };
    }
    var stack = createStack();
    stack.push('2');
    stack.push('3');
    stack.push('5');
    $res = stack.pop();
    console.log($res);

</script>
</body>
</html>